package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.Const;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.widget.SelectConfig;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.rongcloud.rce.lib.model.StaffInfo;

/**
 * 创建工程动态-->谁可以看，选择可以查看本项目的联系人
 * Created by yangshijun on 2018/5/31.
 */

public class EngineerLookActivity extends AppCompatActivity {
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.tv_checked)
    TextView tvChecked;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_moreLinearLayout)
    LinearLayout ivMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.iv_single)
    ImageView ivSingle;
    @BindView(R.id.iv_group)
    ImageView ivGroup;
    @BindView(R.id.topChatDetail)
    LinearLayout topChatDetail;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.iv_open)
    ImageView ivOpen;
    @BindView(R.id.ll_open)
    LinearLayout llOpen;
    @BindView(R.id.iv_select)
    ImageView ivSelect;
    @BindView(R.id.ll_select)
    LinearLayout llSelect;
    @BindView(R.id.contact_list)
    TextView contactList;
    @BindView(R.id.contact_ll)
    LinearLayout contactLl;

    private ArrayList<String> initSelectList = new ArrayList<>();//选中的可见人
    private ArrayList<User> staffs = new ArrayList<>();
    private String userId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_engineer_look);
        ButterKnife.bind(this);
        topBack.setVisibility(View.VISIBLE);
        topText.setText("选择可见人");
        userId = LoginCache.getInstance().getUserInfo().getUserId();
    }

    @OnClick({R.id.topBack, R.id.ll_open, R.id.ll_select})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_open:
                ivOpen.setVisibility(View.VISIBLE);
                ivSelect.setVisibility(View.INVISIBLE);
                contactLl.setVisibility(View.INVISIBLE);
                //清空可见人列表
                initSelectList.clear();
                break;
            case R.id.ll_select:
                ivOpen.setVisibility(View.INVISIBLE);
                ivSelect.setVisibility(View.VISIBLE);
                contactLl.setVisibility(View.VISIBLE);
                Intent intent = new Intent(this, SelectContact2Activity.class);
                SelectConfig selectConfig = new SelectConfig();
                selectConfig.setMaxSelectedNumber(Integer.MAX_VALUE);
                selectConfig.setInitialSelectList(initSelectList);
                intent.putExtra(Const.SELECT_CONFIG, selectConfig);
                startActivityForResult(intent, 100);
                break;
            case R.id.topBack:
                Intent intent2 = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable(SysConstant.msg_key_list1,(Serializable) staffs);
                intent2.putExtras(bundle);
                //intent2.putParcelableArrayListExtra(SysConstant.msg_key_list1, staffs);
                setResult(-1, intent2);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1 & requestCode == 100 & data != null) {
            try {
                staffs = (ArrayList<User>) data.getSerializableExtra(Const.SELECTED_CONTACTS);
               // staffs = data.getParcelableArrayListExtra(Const.SELECTED_CONTACTS);
                if (staffs != null && staffs.size() > 0) {
                    //设置可见人显示
                    setContactList(staffs);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    private void setContactList(ArrayList<User> staffs) {
        StringBuilder builder = new StringBuilder();
        for (User st : staffs) {
            if (st.getServerUserId().equals(userId)){
            }else{
                builder.append(st.getName() + ",");
            }

            initSelectList.add(st.getServerUserId());
        }
        String s = builder.toString();
        contactList.setText(s.substring(0, s.length() - 1));
    }

    @Override
    public void onBackPressed() {
        Intent intent2 = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable(SysConstant.msg_key_list1,staffs);
        intent2.putExtras(bundle);
        //intent2.putParcelableArrayListExtra(SysConstant.msg_key_list1, staffs);
        setResult(-1, intent2);
        super.onBackPressed();
    }
}
